<template>
    <!--时间线-->
    <div style="display: flex">
        <!-- 当日志记录超过8个，可以左右滑动 -->
        <div class="container">
            <div style="display: flex">
                <div style="flex: 1; display: flex; flex-direction: column">
                    <div
                        style="height: 50px; display: flex; align-items: center"
                    >
                        <div style="flex: 1; display: flex">
                            <div style="flex: 1"></div>
                            <div
                                class="move-button el-icon-arrow-left"
                                v-bind:class="{ active: left_button_active }"
                                :disabled="left_button_disabled"
                                @click="moveLeft()"
                            ></div>
                            <div class="item"></div>
                        </div>
                    </div>

                    <div class="item_bottom">
                        <span style="color: white">左</span>
                    </div>
                </div>

                <div
                    v-for="(item, index) in timeLineList"
                    :key="index"
                    v-if="index > point && index <= pointEnd"
                    style="flex: 1; display: flex; flex-direction: column"
                >
                    <div
                        style="height: 50px; display: flex; align-items: center"
                    >
                        <div
                            style="flex: 1; display: flex; align-items: center"
                        >
                            <div class="item"></div>
                            <el-tooltip effect="light" placement="top">
                                <div slot="content">
                                    {{ timeLineList[index].time }}
                                </div>
                                <div
                                    @click="changeActive(index)"
                                    :class="{
                                        active: index === timeIndex,
                                        blod:
                                            timeLineList[index].lineType ===
                                            'blod',
                                        thin:
                                            timeLineList[index].lineType ===
                                            'thin',
                                    }"
                                ></div>
                            </el-tooltip>
                            <div class="item"></div>
                        </div>
                    </div>

                    <div class="item_bottom">
                        <span>{{ timeLineList[index].time }}</span>
                    </div>
                </div>
                <div style="flex: 1; display: flex; flex-direction: column">
                    <div
                        style="height: 50px; display: flex; align-items: center"
                    >
                        <div style="flex: 1; display: flex">
                            <div class="item"></div>
                            <div
                                class="move-button el-icon-arrow-right"
                                v-bind:class="{ active: right_button_active }"
                                :disabled="right_button_disabled"
                                @click="moveRight()"
                            ></div>
                            <div style="flex: 1"></div>
                        </div>
                    </div>

                    <div class="item_bottom">
                        <span style="color: white">右</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'timeLine',
    props: ['timeLineList', 'curTimeIndex'],
    data() {
        return {
            // timeIndex: this.timeLineList.length - 1, //默认当前选择的为最近的时间点
            timeIndex: 0, //默认当前选择的为最近的时间点
            point: 0, //时间轴只展示6个,初始为最后6个
            pointEnd:this.timeIndex + 20,
            right_button_active: false,
            left_button_active: true,
            left_button_disabled: false,
            right_button_disabled: true
        };
    },
    watch: {
        curTimeIndex: {
            handler() {
                this.timeIndex = this.curTimeIndex;
                console.log('this.curTimeIndex', this.curTimeIndex);
                this.point = this.timeIndex > 20 ? this.timeIndex - 20 : 0;
                if (this.timeIndex + 20 < this.timeLineList.length) {
                    this.pointEnd = this.timeIndex + 20;
                } else {
                    this.pointEnd = this.timeLineList.length - 1;
                }
            },
            immediate: true
        }
    },
    methods: {
        changeActive(index) {
            this.timeIndex = index;
            console.log('悬浮选择的时间点：', this.timeIndex);
        },
        moveLeft() {
            if (this.point > 0) {
                this.point -= 1;
                this.pointEnd -= 1;
                this.timeIndex -= 1;
                this.right_button_disabled = false;
                this.right_button_active = true;
                if (this.point === 0) {
                    //如果移到第一个时间点，设置左按钮不可点击
                    this.left_button_disabled = true;
                    this.left_button_active = false;
                }
            }
        },
        moveRight() {
            if (this.pointEnd < this.timeLineList.length - 1) {
                this.point += 1;
                this.pointEnd += 1;
                this.timeIndex += 1;
                this.left_button_disabled = false;
                this.left_button_active = true;
                console.log('————————————', this.timeIndex);
                if (this.pointEnd === this.timeLineList.length - 1) {
                    //如果移到最后一个时间点，设置右按钮不可点击
                    this.right_button_disabled = true;
                    this.right_button_active = false;
                }
            }
        }
    }
};
</script>

<style scoped>
.container {
    width: 800px;
    height: 30px;
    margin-left: 0px;
    padding-left: 0px;
    margin-bottom: 5px;
}

.blod {
    border: 2px solid #dcdfe6;
    border-radius: 20px;
    width: 8px;
    height: 35px;
    background: white;
    margin: 2px 0px;
    box-sizing: border-box;
    cursor: pointer;
}
.thin {
    border: 1px solid #dcdfe6;
    border-radius: 10px;
    width: 4px;
    height: 20px;
    background: white;
    margin: 2px 0px;
    box-sizing: border-box;
    cursor: pointer;
}
.item {
    flex: 1;
    border-bottom: 1px solid #dcdfe6;
    height: 4px;
    /* margin-bottom: 9px; */
    box-sizing: border-box;
}
.item_bottom {
    flex: 1;
    text-align: center;
    height: 15px;
    margin-top: 7px;
    font-size: 14px;
}
.move-button {
    border: 2px solid #dcdfe6;
    width: 40px;
    height: 19px;
    border-radius: 19px;
    background: white;
    text-align: center;
    box-sizing: border-box;
}
.active {
    background-color: #fff !important;
    border: 3px solid #67c23a;
}
</style>